package com.guojian.common.utils;

import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Lists;
import com.guojian.common.format.MyDecimalFormat;
import com.guojian.common.regex.RegexUtils;

public class StringUtilsx extends org.springframework.util.StringUtils {

	private static final DecimalFormat formatInt = new DecimalFormat("#");
	private static final MyDecimalFormat format2Num = new MyDecimalFormat("#.##");
	private static final MyDecimalFormat format2Rate = new MyDecimalFormat("#.##%");

	public static String[] CHAR_32 = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v" };

	/**
	 * 增加了对null字符串的判断
	 *
	 * @param value
	 * @return
	 */
	public static boolean isBlank(Object value) {
		if (value == null) {
			return true;
		}

		if (StringUtils.isBlank(ObjectUtils.toString(value))) {
			return true;
		}

		if ("null".equals(ObjectUtils.toString(value).toLowerCase())) {
			return true;
		}
		return false;
	}

	public static boolean isNotBlank(Object value) {
		return !isBlank(value);
	}

	/**
	 * 去除了所有的空格
	 *
	 * @param value
	 * @return
	 */
	public static String toString(Object value) {
		return StringUtils.deleteWhitespace(ObjectUtils.toString(value));
	}

	public static boolean isNumber(Object data) {
		if (StringUtilsx.isBlank(data)) {
			return false;
		}
		try {
			Double.parseDouble(data.toString());
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	public static int getIntObject(Object num) {
		if (num == null) {
			return 0;
		} else if (isBlank(num)) {
			return 0;
		} else {
			try {
				return Integer.parseInt(formatInt.format(Double.valueOf(toString(num))));
			} catch (NumberFormatException e) {
				return 0;
			}
		}
	}

	public static double getDoubleObject(Object num) {
		if (num == null) {
			return 0d;
		} else if (StringUtils.isBlank(toString(num))) {
			return 0d;
		} else {
			try {
				return Double.parseDouble(toString(num));
			} catch (NumberFormatException e) {
				return 0d;
			}
		}
	}

	public static double getTimeProcess() {
		return 1.00 / DateUtilsx.getNowMonthMaxDay() * DateUtilsx.getYesterdayDateNum();
	}

	public static boolean isIntNum(String num) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(num).matches();
	}

	/**
	 * 获取百分比值
	 *
	 * @param value
	 * @param format
	 * @return
	 */
	public static String getFormatRateValue(Object value, MyDecimalFormat format) {
		if (format == null) {
			format = format2Rate;
		}
		return format.format(value);
	}

	public static String getFormatValue2Rate(Object value) {
		return getFormatRateValue(value, null);
	}

	public static String getFormatValue2Num(Object value) {
		return getFormatRateValue(value, format2Num);
	}

	/**
	 * 从百分比中获取实际数值
	 *
	 * @return
	 */
	public static double getDoubleValueOfRate(Object rate) {
		String rate1 = ObjectUtils.toString(rate);

		if (StringUtils.isBlank(rate1)) {
			return 0;
		}

		if (!rate1.contains("%")) {
			return getDoubleObject(rate1);
		}
		return getDoubleObject(rate1.replaceAll("%", "")) / 100;
	}

	public static String[] getArrayOfString(String params, String regex) {
		return tokenizeToStringArray(params, regex);
	}

	public static String[] getArrayOfString(String params) {
		return getArrayOfString(params, ",");
	}

	public static String getTimestamp() {
		return String.valueOf(System.currentTimeMillis());
	}

	public static String convertString16To32(String hexString) {
		StringBuffer shortBuffer = new StringBuffer();
		int lenth = hexString.length() / 2;
		for (int i = 0; i < lenth; i++) {
			String str16 = hexString.substring(i * 2, i * 2 + 2);
			String char32 = convertChar16To32(str16);
			shortBuffer.append(char32);
		}
		return shortBuffer.toString();
	}

	public static String convertChar16To32(String str16) {
		int x = Integer.parseInt(str16, 16);
		String char32 = CHAR_32[x % 32];
		return char32;
	}

	/**
	 * 驼峰命名字符转换 TESTDATA ====> testdata, TEST_DATA ====> testData, test_data ====>testData
	 *
	 * @param value
	 * @return
	 */
	public static String toLowerCaseOfCamel(String value) {
		Assert.notNull(value, "toLowerCaseOfCamel value 不能为空!");
		return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, value);
	}

	public static String toLowerCaseOfUnderscore(String value) {
		Assert.notNull(value, "toLowerCaseOfUnderscore value 不能为空!");
		return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, value);
	}

	public static boolean isEmptyX(Object object) {
		if (object == null) {
			return true;
		}
		if (object instanceof CharSequence) {
			CharSequence cs = (CharSequence) object;
			return !hasText(cs) || "null".equalsIgnoreCase(cs.toString());
		}
		if (object.getClass().isArray()) {
			return Array.getLength(object) == 0;
		}
		if (object instanceof Collection<?>) {
			return ((Collection<?>) object).isEmpty();
		}
		if (object instanceof Map<?, ?>) {
			return ((Map<?, ?>) object).isEmpty();
		}
		return false;
	}

	public static boolean isNotEmptyX(Object object) {
		return !isEmptyX(object);
	}

	/**
	 * format("this is {}, and i am very {} it", "1", "2") = this is  1, and i am very 2 it
	 * 
	 * @param input
	 * @param args
	 * @return
	 */
	public static String format(String input, Object... args) {
		String newStr = input;
		for (Object arg : args) {
			Matcher matcher = RegexUtils.REGEX_PATTERN_DKH.matcher(newStr);
			newStr = matcher.replaceFirst(ObjectUtils.toString(arg));
		}
		return newStr;
	}

	public static String collectionToDelimitedString(@Nullable Collection<?> coll, String delim, String prefix, List<?> suffixs) {
		if (CollectionUtils.isEmpty(coll)) {
			return StringUtils.EMPTY;
		}
		if (CollectionUtils.isEmpty(suffixs)) {
			return StringUtils.EMPTY;
		}
		if (coll.size() != suffixs.size()) {
			throw new IllegalArgumentException("两个集合的大小必须一致");
		}
		StringBuilder sb = new StringBuilder();
		Object[] cols = coll.toArray();
		for (int i = 0; i < cols.length; i++) {
			sb.append(prefix).append(cols[i]).append(suffixs.get(i));
			if (i != cols.length - 1) {
				sb.append(delim);
			}
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		int a = 1001;
		Integer b = 1001;
		System.out.println(a == b);
		System.out.println(formatInt.format(Double.valueOf(12212)));
		System.out.println(StringUtilsx.getFormatRateValue(-2.6321525885558583, new MyDecimalFormat()));
		System.out.println(getFormatValue2Rate("0.0026"));
		System.out.println(StringUtilsx.getDoubleValueOfRate("0.26%"));

		System.out.println("b = " + System.currentTimeMillis() + "" + RandomUtils.nextInt(1000000));
		System.out.println("getTimestamp() = " + getTimestamp());
		System.out.println("getTimeProcess() = " + getTimeProcess());

		System.out.println("String.join(\",\",\"a\",\"b\") = " + String.join(",", "a", "b"));
		System.out.println(toLowerCaseOfUnderscore("RoleUser"));

		List<String> zw = Lists.newArrayList();
		/*zw.add("?");
		zw.add("?");
		zw.add("?");
		zw.add("?");
		zw.add("?");*/

		System.out.println(StringUtilsx.collectionToCommaDelimitedString(zw));

		System.out.println(StringUtilsx.isEmptyX("null"));

		System.out.println(format("this is  {}, and i am very {} it", "1", "2"));
		
		
		
		
		

	}

}
